-- draw tools




function render_view(player, map)


	for i = 1, #map.tiles do

		map.tiles[i].view = false

	end


	draw_surround(8, player.x, player.y, map.layout, map.tiles)

	draw_viewed_tiles(map.tiles)



end



function draw_surround(res, tar_x, tar_y, grid, tiles)
	resolution = res
	size_inc = 1
	total_inc = size_inc * resolution
	total_selected = 0

	local x1
	local y1

	local tX = tar_x/tile_size
	local tY = tar_y/tile_size



		for x = 1, total_inc do
			for y = 1, total_inc do
				grid_to_tile(x, y, tX, tY, tiles)
			end
		end



end



function grid_to_tile(x, y, tX, tY, tiles)


	local x2 = (tX+x) * tile_size
	local y2 = (tY+y) * tile_size


	local x3 = (tX-x) * tile_size
	local y3 = (tY-y) * tile_size


	local x4 = (tX+x) * tile_size
	local y4 = (tY-y) * tile_size


	local x5 = (tX-x) * tile_size
	local y5 = (tY+y) * tile_size


	local x6 = tX * tile_size
	local y6 = tY * tile_size

	local x7 = tX * tile_size
	local y7 = (tY-y) * tile_size


	local x8 = tX * tile_size
	local y8 = (tY+y) * tile_size


	local x9 = (tX+x) * tile_size
	local y9 = tY * tile_size


	local x1 = (tX-x) * tile_size
	local y1 = tY * tile_size



	for i = 1, #tiles do
		if tiles[i].x == x2 and tiles[i].y == y2 then
			tiles[i].view = true

		elseif tiles[i].x == x3 and tiles[i].y == y3 then
			tiles[i].view = true

		elseif tiles[i].x == x4 and tiles[i].y == y4 then
			tiles[i].view = true

		elseif tiles[i].x == x5 and tiles[i].y == y5 then
			tiles[i].view = true

		elseif tiles[i].x == x6 and tiles[i].y == y6 then
			tiles[i].view = true

		elseif tiles[i].x == x7 and tiles[i].y == y7 then
			tiles[i].view = true

		elseif tiles[i].x == x8 and tiles[i].y == y8 then
			tiles[i].view = true

		elseif tiles[i].x == x9 and tiles[i].y == y9 then
			tiles[i].view = true

		elseif tiles[i].x == x1 and tiles[i].y == y1 then
			tiles[i].view = true

		end

	end

end


function draw_viewed_tiles(tiles)

	for i = 1, #tiles do
	--print(table.tostring(tiles[i]))
		if tiles[i].view == true then
			--print(table.tostring(tiles[i]))

			if tiles[i].image:typeOf("Drawable") then

				align_tile(tiles[i])
				love.graphics.draw(tiles[i].image,
					tiles[i].drawX,
					tiles[i].drawY)

			elseif tiles[i].image:typeOf("Quad") then

				align_tile(tiles[i])

				love.graphics.drawq(env_sheet_img,
						tiles[i].image,
						tiles[i].drawX,
						tiles[i].drawY,
						0, 2.0, 2.0)

			end

		end


	end

end

function align_tile(tile)

	local x1, y1 = additive_range(data.player.x, data.player.y,
		tile.x, tile.y)

	local x2 = x1/scale_size
	local y2 = y1/scale_size

	tile.drawX = data.player.drawX+x1
	tile.drawY = data.player.drawY+y1


end
